package com.swordget.exception;

import com.swordget.entity.vo.SimpleResponseVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import static org.springframework.http.HttpStatus.OK;

/**
 * on 2020/4/5 13:35
 * 全局异常处理
 * @author David
 */
@ControllerAdvice
public class ExceptionHandler {

    private final static Logger log = LoggerFactory.getLogger(ExceptionHandler.class);

    @org.springframework.web.bind.annotation.ExceptionHandler
    @ResponseStatus(code = OK)
    @ResponseBody
    public SimpleResponseVo handlerVerifyFailException(VerifyFailException ex) {
        this.writerLog(ex);
        return SimpleResponseVo.fail("处理失败:" + ex.getMessage());
    }

    @org.springframework.web.bind.annotation.ExceptionHandler
    @ResponseStatus(code = OK)
    @ResponseBody
    public SimpleResponseVo handlerResolveFailException(ResolveException ex) {
        this.writerLog(ex);
        return SimpleResponseVo.fail("处理失败:" + ex.getMessage());
    }

    @org.springframework.web.bind.annotation.ExceptionHandler
    @ResponseStatus(code = OK)
    @ResponseBody
    public SimpleResponseVo handlerDataCheckException(DataCheckException ex) {
        this.writerLog(ex);
        //前台判断这种情况下载错误文件
        return SimpleResponseVo.fail("000002", ex.getMessage());
    }

    @org.springframework.web.bind.annotation.ExceptionHandler
    @ResponseStatus(code = OK)
    @ResponseBody
    public SimpleResponseVo handlerException(Exception ex) {
        this.writerLog(ex);
        return SimpleResponseVo.fail("处理失败");
    }





    private void writerLog(Throwable t) {
        t.printStackTrace();
        log.error("捕获到异常，异常信息:", t);
    }


}
